var gamename = "";
var servername = "";
var game_id = 0;
var area_num = 0;
var username = '';
var username_to = '';
var gametag = '';
var money = '';
var exchange = 0;
var payid = 1;
var bankname = '';
$(function () {

    init_typelist();
    set_typelist_class(0);
    set_current_typename(0);

    $("#tlist li").click(function (e) {
        payid = $(this).find('span').attr('data-key');
        set_typelist_class(payid - 1);
        set_current_typename(payid - 1);
        if (payid != 1) {
            $("#select_bank").hide();
        } else {
            $("#select_bank").show();
        }
    });

    // ABC DEF
    $("#glist dd").each(function (i) {
        $(this).find('a').click(function (e) {
            e.preventDefault();
            var letterid = $(this).attr('data-key');
            get_letterid_game(letterid);
        });
    });

    if ($.cookie('webgame')) {
        username = $.cookie('webgame');
        username_to = $.cookie('webgame');
        $('#username').val(username);
        $('#username_to').val(username);
    }

    $("#username").blur(function () {
        if (username == '') {
            username = $("#username").val();
        }
    });
    $("#username_to").blur(function () {
        if (username_to == '') {
            username_to = $("#username_to").val();
        }
        //check_user();
    });

    $("#recharge_money a").click(function (e) {
        e.preventDefault();
        $(this).addClass('on').siblings().removeClass('on');
        money = $(this).attr('data-key');
        $("#exchange").val(converter(money))
    });

    $("#towmoney").click(function () {
        if (money != 0) {
            if (confirm('你已经选充值金额，是否在重新输入？'))
            {
                $("#recharge_money a").each(function () {
                    $(this).removeClass('on');
                });
                $("#exchange").val(0);
                money = 0;
            }
        }
    });
    $("#towmoney").blur(function () {
        if (money != 0) {
            return false;
        } else {
            money = $(this).val();
            $("#exchange").val(converter(money))
            //console.log(money);
        }
    });

    $("#select_bank a").click(function (e) {
        e.preventDefault();
        $(this).addClass('on').siblings().removeClass('on');
        bankname = $(this).attr('data-key');
    });
    $("#payconfirm").click(function () {
        if (game_id == 0) {
            alert("请选择要充值的游戏！");
            $("#game_name").focus();
            return;
        }
        if (area_num == 0) {
            alert("请选择要充值的区服！");
            $("#server_name").focus();
            return;
        }
        if (username == '') {
            alert("请输入要充值的账号！");
            $("#username").focus();
            return;
        }
        if (username_to == '') {
            alert("请输入要充值的账号！");
            $("#username_to").focus();
            return;
        }
        if (money == 0) {
            alert("请选择充值的金额！");
            return;
        }
        payurl = domain + "/index.php?s=/pay/confirm&payid=" + payid + "&game_id=" + game_id + "&area_num=" + area_num + "&username=" + username + "&username_to=" + username_to + "&money=" + money + "&bankname=" + bankname;
        window.location.href = payurl;
    });
});

/**
 * 根据gid获取服务器列表
 * @param {type} gid
 * @param {type} gamename
 * @returns {undefined}
 */
function get_slist(gid, gname) {
    gamename = gname;
    game_id = gid;
    $("#recharge_game_name").text(gamename);
    $("#game_name").val(gamename);
    //console.log(gid);
    var param = 'game_id=' + gid;
    $.ajax({
        type: 'post',
        url: domain + '/index.php?s=/pay/serverlist',
        data: param,
        dataType: 'json',
        success: function (data) {
            var html = '';
            for (i = 0; i < data.length; i++) {
                html += '<li><a href="javascript:void(0)" onclick="set_slist(\'' + data[i]['area_num'] + '\',\'' + data[i]['areaname'] + '\')">' + data[i]['areaname'] + '</a></li>';
            }
            $("#serverlist").html(html);
            $("#server_list").show();
            $("#game_list").hide();
        }
    });
}

function set_slist(sid, areaname) {
    servername = areaname;
    area_num = sid;
    gametag = gamename + "--" + servername;
    $("#recharge_game_name").text(gametag);
    $("#server_name").val(servername);
    $("#server_list").hide();
    $.ajax({
        type: 'post',
        url: domain + '/index.php?s=/pay/getexchange',
        data: {'game_id': game_id},
        dataType: 'json',
        success: function (data) {
            //console.log(data);            
            exchange = data;
        }
    });
}

function check_user() {
    //检查用户名，角色是否存在
    var param = 'username=' + username_to + '&game_id=' + game_id + '&area_num=' + area_num;
    $.ajax({
        type: 'post',
        url: domain + '/index.php?s=/pay/check_user',
        data: param,
        dataType: 'json',
        success: function (data) {
            if (data['ret'] == 0) {
                $("#msg").html('<font color="green">' + data['msg'] + '</font>');
            } else {
                $("#msg").html('<font color="red">' + data['msg'] + '</font>');
            }
        }
    });
}
/**
 * 根据字母分类切换游戏
 * @param {type} letterid
 * @returns {undefined}
 */
function get_letterid_game(letterid) {
    var param = 'letterid=' + letterid;
    $.ajax({
        type: 'post',
        url: domain + '/index.php?s=/pay/gamelist',
        data: param,
        dataType: 'json',
        success: function (data) {
            var html = '';
            for (i = 0; i < data.length; i++) {
                html += '<li><a href="javascript:void(0)" onclick="get_slist(\'' + data[i]['id'] + '\',\'' + data[i]['gamename'] + '\')">' + data[i]['gamename'] + '</a></li>';
            }
            $("#gamelist").html(html);
        }
    });
}


/**
 * 设置当前渠道样式
 * @param {type} index li的索引
 * @returns {undefined}
 */
function set_typelist_class(index) {
    $("#tlist li").eq(index).find('span').addClass('on');
    $("#tlist li").eq(index).siblings().find('span').removeClass('on');
}

/**
 * 设置当前渠道名称
 * @param {type} index
 * @returns {undefined}
 */
function set_current_typename(index) {
    $("#current_type").text(typelist[index]['typename']);
}

/**
 * 初始化渠道列表
 * @returns {undefined}
 */
function init_typelist() {
    var html = '';
    for (i = 0; i < typelist.length; i++) {
        html += '<li><span data-key=' + typelist[i]['id'] + '>' + typelist[i]['typename'] + '</span></li>';
    }
    $("#tlist").html(html);
}

function show_div(id) {
    $("#" + id).show();
}

function hide_div(id) {
    $("#" + id).hide();
}


function converter(v) {
    if (payid == 3)    //  神州行
    {
        v = v * 0.97;
    }
    if (payid == 4 || payid == 5) { //联通电信
        v = v * 0.96
    }
    if (payid == 6 || payid == 7) {//骏网一卡通Q币卡
        v = v * 0.84;
    }
    v = Math.floor(v) * exchange;
    return v;
}